import SoundManager from "../Managers/SoundManager";
import AndroidTool, { ADEnum } from "../Tools/AndroidTool";
import Banner from "../Tools/Banner";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MainUI extends cc.Component {
    private static instance:MainUI;
    static get Instance(){
        return this.instance;
    }

    @property(cc.Node)
    StartButton:cc.Node;
    @property(cc.Node)
    Levels:cc.Node;
    @property(cc.Node)
    LevelSelectionPanel:cc.Node;
    @property(cc.Node)
    UnlockPanel:cc.Node;
    @property(cc.Node)
    HidePanel:cc.Node;
    @property(cc.Node)
    yes:cc.Node;
    @property(cc.Node)
    accept:cc.Node;
    @property(cc.Node)
    refuse:cc.Node;

    levelNum = -1;
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        MainUI.instance = this;
    }

    start () {
        this.StorageInit();
        this.LevelInit();
        SoundManager.Instance.PlayBgm();
        Banner.Instance.showBanner();
    }

    // update (dt) {}

    Start(){
        this.LevelSelectionPanel.active = true;
        this.StartButton.active = false;
        SoundManager.Instance.PlayButton();
        AndroidTool.ShowAD(ADEnum.ChaPing);
        Banner.Instance.CreateCustomAd();
    }

    StorageInit(){
        if(!localStorage.getItem("LevelUnlock")) localStorage.setItem("LevelUnlock", "0");
        // if(!localStorage.getItem("Answer")) localStorage.setItem("Answer", JSON.stringify([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
        // if(!localStorage.getItem("Hide")){
        //     this.Hide();
        //     this.yes.active = false;
        //     this.accept.active = true;
        //     this.refuse.active = true;
        // }
    }

    LevelInit(){
        var unlock = parseInt(localStorage.getItem("LevelUnlock"));
        for(var i = 1; i < this.Levels.children.length; i++){
            var level = this.Levels.children[i];
            if(i <= unlock) {
                var lock = level.children[1];
                lock.active = false;
            }
        }
    }

    Back(){
        this.StartButton.active = true;
        this.LevelSelectionPanel.active = false;
        SoundManager.Instance.PlayButton();
        Banner.Instance.CreateCustomAd();
    }

    Play(event:cc.Event){
        AndroidTool.ShowAD(ADEnum.ChaPing);
        Banner.Instance.CreateCustomAd();
        var target:cc.Node = event.target;
        var num = this.Levels.children.indexOf(target);
        if(num == -1) console.error("你这按钮不对啊");
        else{
            this.levelNum = num;
            // console.log(this.levelNum);
            if(parseInt(localStorage.getItem("LevelUnlock")) >= num) {
                cc.director.loadScene("GameScene");
                SoundManager.Instance.PlayButton();
            }
            else{
                this.UnlockPanel.active = true;
                this.LevelSelectionPanel.active = false;
            }
        }
    }
    
    onUnlockYes(){
        // this.onUnlockReward();
        SoundManager.Instance.PlayButton();
        // Banner.Instance.CreateVideo(this.onUnlockReward);
        AndroidTool.ShowAD(ADEnum.GameVideo1);
    }

    onUnlockReward(){
        MainUI.Instance.UnlockPanel.active = false;
        localStorage.setItem("LevelUnlock", MainUI.Instance.levelNum.toString());
        MainUI.Instance.LevelInit();
        MainUI.Instance.Start();
    }

    onUnlockNo(){
        this.UnlockPanel.active = false;
        this.LevelSelectionPanel.active = true;
        SoundManager.Instance.PlayButton();
    }

    Hide(){
        AndroidTool.ShowAD(ADEnum.YinSi);
        // this.HidePanel.active = true;
    }

    Kefu(){
        AndroidTool.ShowAD(ADEnum.KeFu);
    }

    More(){
        AndroidTool.ShowAD(ADEnum.MoreGame);
    }

    HideYes(){
        localStorage.setItem("Hide", "1");
        this.yes.active = true;
        this.accept.active = false;
        this.refuse.active = false;
        this.HidePanel.active = false;
    }

    HideNo(){
        qg.exitApplication({
            success: function () {
                console.log("exitApplication success");
            },
            fail: function () {
                console.log("exitApplication fail");
            },
            complete: function () {
                console.log("exitApplication complete");
            }
        });
    }

    HideBack(){
        this.HidePanel.active = false;
    }
}
